package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.entity.Student;
import com.hzit.service.IStudentService;
import com.hzit.service.impl.StudentServiceImpl;
import com.hzit.vo.StudentVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Objects;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private IStudentService studentService;
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		//1. 得到cmd参数
		String cmd = req.getParameter("cmd");
		//2. 根据参数的值，决定调用不同的方法
		if(StrUtil.isNotBlank(cmd)){    // 相当于：cmd != null && !"".equals(cmd)
			if("list".equals(cmd)){     // 列表学生
				list(req,resp);
			}
			if("findByPage".equals(cmd)){  // 分页查询
				findByPage(req,resp);
			}
			if("search".equals(cmd)){      // 条件查询带分页
				search(req,resp);
			}
			if("add".equals(cmd)){         // 添加学生
				add(req,resp);
			}
			if("update".equals(cmd)){      // 修改学生
				update(req,resp);
			}
		}
	}
	// 修改学生
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 通过jackson-databind插件，将前端传入的查询条件转换为StudentVo对象
		ObjectMapper objectMapper = new ObjectMapper();
		StudentDTO dto = objectMapper.readValue(req.getInputStream(), StudentDTO.class);
		R<Object> r = null;
		try {
			//2. 修改学生
			studentService.update(dto);
			//3. 构造一个R对象
			r = R.ok(null,"修改学生成功!");
		} catch (Exception e) {
			e.printStackTrace();
			r = R.fail("修改学生失败！");
		}finally {
			resp.getWriter().println(JSON.toJSONString(r));
			resp.getWriter().flush();
		}
	}
	// 添加学生
	private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 通过jackson-databind插件，将前端传入的查询条件转换为StudentVo对象
		ObjectMapper objectMapper = new ObjectMapper();
		StudentDTO dto = objectMapper.readValue(req.getInputStream(), StudentDTO.class);
		R<Object> r = null;
		try {
			//2. 添加学生
			studentService.save(dto);
			//3. 构造一个R对象
			 r = R.ok(null,"添加学生成功!");
		} catch (Exception e) {
			e.printStackTrace();
			r = R.fail("添加学生失败！");
		}finally {
			resp.getWriter().println(JSON.toJSONString(r));
			resp.getWriter().flush();
		}
	}

	// 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 通过jackson-databind插件，将前端传入的查询条件转换为StudentVo对象
		ObjectMapper objectMapper = new ObjectMapper();
		StudentVo studentVo = objectMapper.readValue(req.getInputStream(), StudentVo.class);
		//2. 获取分页参数
		Integer page = new Integer(req.getParameter("page"));
		Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
		//3. 开始条件查询带分页
		PageResult<StudentDTO> pr = studentService.search(page,pageSize,studentVo);
		//4. 使用r对象包装
		R<PageResult<StudentDTO>> r = R.ok(pr);
		//5. 转换为字符串再输出
		resp.getWriter().println(JSON.toJSONString(r));
		resp.getWriter().flush();
	}

	// 分页查询
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到当前页
		int page = Integer.parseInt(req.getParameter("page"));
		//2. 每页大小
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		//3. 开始分页查询
		PageResult<StudentDTO> pr = studentService.findByPage(page,pageSize);
		//4. 使用R对象封装
		R<PageResult<StudentDTO>> r = R.ok(pr);
		String s = JSON.toJSONString(r);
		//5. 将json串输出到前端
		PrintWriter writer = resp.getWriter();
		writer.println(s);
		writer.flush();
	}
	// 查询所有学生
	private void list(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//1. 查询出所有学生列表
		List<Student> students = studentService.findAll();
		//2. 将上面的封装到r对象中，转换为字符串
		R<List<Student>> r = R.ok(students);
		String s = JSON.toJSONString(r);
		//3. 将json串输出到前端
		PrintWriter writer = resp.getWriter();
		writer.println(s);
		writer.flush();
	}
}
